import com.fasterxml.jackson.databind.ObjectMapper;
import com.nicico.copper.common.util.date.DateUtil;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.util.*;
import java.util.stream.IntStream;

public class IranHolidays {
    private File getFileFromResources(String fileName) {

        ClassLoader classLoader = getClass().getClassLoader();

        URL resource = classLoader.getResource(fileName);
        if (resource == null) {
            throw new IllegalArgumentException("file is not found!");
        } else {
            return new File(resource.getFile());
        }

    }

    public static Map<String,List<List<Integer>>> getHolidays() throws IOException {
        IranHolidays iranHolidays = new IranHolidays();
        ObjectMapper objectMapper = new ObjectMapper();
        Map map = new HashMap();
        File fileFromResources = iranHolidays.getFileFromResources("holidays-iran.json");
          map   = objectMapper.readValue(fileFromResources, Map.class);
            Map holidays = (Map) map.get("holidays");
            List<List<Integer>> jalaliHolidays = (List) holidays.get("jalali");
            final List<List<Integer>> hijriHolidays = (List) holidays.get("hijri");
            final List<List<Integer>> jalaliHoldiydaysList = new ArrayList<List<Integer>>(){{
                for(int i=0;i<13;i++) add(new ArrayList<Integer>());
            }};
            jalaliHoldiydaysList.get(1).add(1);
            final ArrayList<List<Integer>> hijriHolidaysList = new ArrayList<List<Integer>>(jalaliHoldiydaysList);
            for (List<Integer> hd: hijriHolidays
                 ) {
                hijriHolidaysList.get(hd.get(0)).add(hd.get(1));
            }
            for (List<Integer> hd: jalaliHolidays
                 ) {
                jalaliHoldiydaysList.get(hd.get(0)).add(hd.get(1));
            }


            return new HashMap<String, List<List<Integer>>>(){{
                put("jalali",jalaliHoldiydaysList);
                put("hijri",hijriHolidaysList);
            }};

    }

    private Map<Integer, Map<Integer, Map<Integer, Integer>>> xlsx2json(String fileName,
                                                                              String sheetName,
                                                                              String saveJsonPathAddress) throws IOException {
        String datesJson= "";
        ObjectMapper objectMapper = new ObjectMapper();
        IranHolidays iranHolidays = new IranHolidays();
        File xlsxFile = iranHolidays.getFileFromResources(fileName);
        Map<Integer,Map<Integer,Map<Integer,Integer>>> datesMap = new HashMap(){{
            IntStream.range(1,13).forEach(i-> put(i, new HashMap<Integer,List<Integer>>()));
        }};
            FileInputStream fileInputStream = new FileInputStream(xlsxFile);
            XSSFWorkbook workbook = new XSSFWorkbook(fileInputStream);

            XSSFSheet sheet = sheetName != null ? workbook.getSheet(sheetName): workbook.getSheetAt(0);
            Iterator<Row> rows = sheet.iterator();
            rows.next(); // first row is header
            while (rows.hasNext()){
                Row row = rows.next();
                Integer jMonth = Integer.valueOf((int) row.getCell(0).getNumericCellValue());
                Integer hMonth = Integer.valueOf((int) row.getCell(2).getNumericCellValue());
                Integer jDay = Integer.valueOf((int) row.getCell(1).getNumericCellValue());
                Integer hDay = Integer.valueOf((int) row.getCell(3).getNumericCellValue());
                Map<Integer, Map<Integer, Integer>> jMonthMap = datesMap.get(jMonth);
                jMonthMap.put(

                        jDay,
                         new HashMap<Integer, Integer>(){{put(hMonth,hDay);}}
                        );



            }
            datesJson = objectMapper.writeValueAsString(datesMap);
            objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
            objectMapper.writeValue(new File(saveJsonPathAddress),datesMap);


        return datesMap;
    }

    public static Boolean isHoliday(Integer year,Integer month, Integer day , Boolean isJalali) throws IOException {
        IranHolidays iranHolidays= new IranHolidays();
        if (!isJalali){
            String[] jDate = DateUtil.convertMiToKh(year, month, day).split("/");
            year = Integer.valueOf(jDate[0]);
            month = Integer.valueOf(jDate[1]);
            day = Integer.valueOf(jDate[2]);
        }
        Map<String, List<List<Integer>>> holidays = getHolidays();
        File fileFromResources = iranHolidays.getFileFromResources(String.format("jalali2hijri/%d.json", year));
        ObjectMapper objectMapper = new ObjectMapper();
        Map<Integer,Map<Integer,Map<Integer,Integer>>> jalali2hijri = objectMapper.readValue(fileFromResources, Map.class);
        Map<Integer, Integer> hDate = jalali2hijri.get(month).get(day);
        hDate.keySet().toArray();


        return null;
    }

    public static void main(String[] args) {
    final String  saveJsonPathAddress = "C:\\Users\\shahrbabak\\IdeaProjects\\iranHolidays\\src\\main\\resources\\jalali2hijri\\1399.json";
        try {
            IranHolidays.isHoliday(2020,04,01,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}